﻿using System;
using System.Data;
using System.Collections;
using System.Collections.Specialized;

using Common;

namespace com.globalwave.system
{
    public partial class PrivilegePage : Common.BasePage
    {
        public PrivilegePage()
            : base()
        {
            entityName = "Privilege";
        }

        public void list() {

            NameValueCollection criterion = getCriterion();

            DataSet ds = PrivilegeService.getInstance().query(criterion, pageIndex, pageSize,null);

            renderListXML(ds);
        }

        public void create()
        {
            NameValueCollection privilege = getPrivilege();

            int privilege_id =
                PrivilegeService.getInstance().create(privilege);

            privilege.Set("privilege_id", privilege_id.ToString());

            renderSuccessCreateMessage(privilege);
            
        }
        
        public void update()  {

            NameValueCollection privilege = getPrivilege();

            PrivilegeService.getInstance().update(privilege);


            renderSuccessUpdateMessage(privilege);
            
        }
        
        public void delete()  {
            string[] privilege_ids = this.getPrivilege_ids();

            if (privilege_ids == null) {
                PrivilegeService.getInstance().delete(getPrivilege());
            } else {
                PrivilegeService.getInstance().deleteAll(privilege_ids);
            }

            renderSuccessDeleteMessage();
            
        }

        public void possessedByUser() {
            DataSet ds = PrivilegeService.getInstance().possessedByUser(Request.Params.Get("user_id"));

            renderXML(ds);        

            
        }

        public void possessedByOrganization() {
            DataSet ds = PrivilegeService.getInstance().possessedByOrganization(Request.Params.Get("organization_id"));

            renderListXML(ds);

        }

        private NameValueCollection getCriterion()
        {

            return U.filter(Request.Params, "privilegeCriterion")[0];
        }

        private NameValueCollection getPrivilege()
        {

            NameValueCollection privilege = U.filter(Request.Params, "privilege")[0];
            if ("".Equals(privilege.Get("pro_privilege_id"))) {
                privilege.Set("pro_privilege_id", null);
            }
            return privilege;
        }
        private string[] getPrivilege_ids() {
            return Request.Params.GetValues("privilege_ids");
        }

        private static Hashtable privilegeMap = null;

        override protected Hashtable getPrivilegeMap()
        {
            if (privilegeMap == null)
            {
                privilegeMap = new Hashtable();

                privilegeMap.Add("login", (short)-1);
                privilegeMap.Add("list", (short)-1);
                privilegeMap.Add("create", (short)-1);
                privilegeMap.Add("update", (short)-1);
                privilegeMap.Add("delete", (short)-1);
                privilegeMap.Add("possessedByOrganization", (short)-1);
                privilegeMap.Add("possessedByUser", (short)-1);
            }

            return privilegeMap;
        }
    }
}